<?php
require_once 'CMS/Util.php';
require_once 'CMS/Pagination.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language
/*
Người tạo	: Lại Ngọc Thái Châu - Team 0
Module 	: Comment Product
Chức năng	: Show + Post Comment
 */

class Comment_IndexController {
	private $pagination;
	public function init(){
		parent::init();

	}

	public function indexAction()
	{
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/comment');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
		$this->view->textlist		= $language->get('text_list');
		$this->view->textcreatedday = $language->get('text_created_day');
		$this->view->textupdatedday = $language->get('text_updated_day');
		$this->view->textsend 		= $language->get('text_send');
		$this->view->textanswer	    = $language->get('text_answer');
		$this->view->textbtnsend	= $language->get('text_btn_send');
		$this->view->textnocm	    = $language->get('text_no_cm');
		$this->view->text_remain_char= $language->get('text_remain_char');
		$this->view->comfirm_delete	= $language->get('comfirm_delete');
		
		$this->view->text_safe_key	= $language->get('text_safe_key');
		$this->view->text_text	    = $language->get('text_text');
		$this->view->textError1 	= $language->get('error1');
		$this->view->textError2 	= $language->get('error2');
		
		$this->view->alert1	= $language->get('alert1');
		$this->view->alert2	= $language->get('alert2');
		$this->view->alert3	= $language->get('alert3');
		$this->view->alert4	= $language->get('alert4');
		/*========== End Text ==============*/
		
		//---------------------- Khai báo các parameter----------------------//
		$this->pagination = new Pagination();		
		$dbUser = new Comment_Model_Users();
		$dbComment = new Comment_Model_Comment();
		$dateInt = new Util();
		
		$path = APPLICATION_PATH."/widgets/comment/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		 
            
		/*-------- Start kiem tra id mat hang  -------------*/
		if(isset($_GET['id']))
		{
			if(is_numeric($_GET["id"]))
			{
			
				// gan gia tri thing_id
				$id = $_GET["id"];
				
				$countComment = $dbComment->countCommentbyID($id);
	            
	            /*-------- Start kiem tra phan trang  -------------*/
	            // kiem tra xem dang o page may
				if (!isset($_GET["page"]) || is_numeric($_GET["page"])!=true ||$_GET["page"]<0) 
	            	$page = 1;
	            else 
	            	$page = $_GET["page"];
	            	
	            // so comment tren 1 trang
	            $limit = 5;
	            
	            // khoi tao phan trang
	            $this->pagination->init("/comment/comment/postcomment?id=". $_GET["id"]  ."&page=", $countComment, $limit, $page);
	            
				// phan trang ban ajax
	            $this->view->pagination = $this->pagination->renderjson();
				  
	            $dataComment = $dbComment->getCommentbyID($id, $page, $limit);
				
				/*-------- End kiem tra phan trang  -------------*/
				$i=0;
				if($dataComment!=null)
				{
					foreach($dataComment as $data)
					{
						$comment[$i]=array(
						"comment_id" =>$data['comment_id'],
						"title" =>$data['title'],
						"description" =>$data['description'],
						"created_date" =>$dateInt->makeDays($data['created_date']),
						"updated_date" =>$dateInt->makeDays($data['updated_date']),
						"user_id" =>$data['user_id'],
						"user_link" =>$data['user_link'],
						"user_image" =>$data['user_image'],
						"user_name" =>$data['user_name'],
						"yahoo" =>$data['yahoo'],
						"skype" =>$data['skype'],
						);
						$i++;
					}
					$this->view->datacomment = $comment;
				}
					
				$this->view->countcomment = $countComment;
			}
		}
		else 
		{
			$this->view->datacomment = null;
		}
		/*-------- End kiem tra id mat hang  -------------*/
		
		/*-------- Start kiem tra login -------------*/
		$session = new Zend_Session_Namespace('login.auth');
		if(isset($session->id_user))
		{
			$this->view->logged=1;
			$user_info= $dbUser->selectUserAvartaByUserID($session->id_user);
			$this->view->current_user_image = $user_info;
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else
		{
			$this->view->logged=0;
		}
		/*-------- End kiem tra login -------------*/
		
		if(isset($_REQUEST["security_code"]) && isset($_REQUEST["code"]))
		{
			$captchaID = $_REQUEST["code"];
			$txtCaptcha =$_REQUEST["security_code"];
			//Start------Lấy thông tin captcha từ session
	       	$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaID);
	       	$captchaIterator = $captchaSession->getIterator();
	       	$captchaWord = $captchaIterator['word'];
         	if($captchaWord==null && $session->current_code!=null )
         	$captchaWord = $session->current_code;
         	//End--------Lấy thông tin captcha từ session
	  
	         	
	       	//Start------Kiểm tra captcha nhập từ form
			if(strtolower($txtCaptcha) != strtolower($captchaWord)){
				$flag = false;
				$session->current_code = $captchaWord;
				$data =-1;
			}
			else 
			{
				$data =1;
			}
			//End--------Kiểm tra captcha nhập từ form
			
			$this->_helper->json($data);
		}
		else
		{
				
			//Start------Khởi tạo mã captcha
		    $captcha = new Zend_Captcha_Image();
			$captcha	->setTimeout('300')
		                ->setWordLen('6')
		                ->setHeight('50')
		                ->setWidth('160')
		                ->setImgDir(APPLICATION_PATH . '/../public/captcha/images/')
		                ->setImgUrl('/captcha/images/')
		                ->setSuffix('.jpg')
		                ->setFont(APPLICATION_PATH . '/../public/captcha/font/jkasperville.ttf')
		                ->setFontSize('30')
		                ->setLineNoiseLevel('5')
		            	->setDotNoiseLevel('50')
		            	->setExpiration('1')
		           	 	->setGcFreq('1');
			$captcha->generate();
			$this->view->captcha = $captcha->render(); 
			$this->view->captchaID = $captcha->getId();
			//End--------Khởi tạo mã captcha
				
			//Start------Đưa chuỗi captcha vào session
			$captchaSession = new Zend_Session_Namespace('Zend_Captcha_' . $captcha->getId());
			$captchaSession->word = $captcha->getWord();
			//End--------Đưa chuỗi captcha vào session	
		}
			
	}
}